﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace DAO
{
    public class ThanhVienDAO : DataAccess
    {
        public ThanhVien CreateObj(DataRow row)
        {
            ThanhVien obj = new ThanhVien();
            obj.MaTV = Utils.ToStr(row["MaTV"]);
            obj.NgaySinh = Utils.ToDataTime(row["NgaySinh"]);
            obj.SDT = Utils.ToStr(row["SDT"]);
            obj.TenTV = Utils.ToStr(row["TenTV"]);
            //obj.TienNo = Utils.ToFloat(row["TienNo"]);
            obj.CV = Utils.ToInt(row["CV"]);
            obj.DiaChi = Utils.ToStr(row["DiaChi"]);
            return obj;
        }
        public List<ThanhVien> GetList()
        {
            DataTable tb = GetData("EXEC GetData ?", "@TableNameX", "ThanhVien", OleDbType.VarChar);
            List<ThanhVien> LstThanhVien=new List<ThanhVien>();
            for(int i=0;i<tb.Rows.Count;i++)
            {
                LstThanhVien.Add(CreateObj(tb.Rows[i]));
            }
            return LstThanhVien;
       }
        public bool Insert(ThanhVien obj)
        {
            obj.MaTV = Utils.ToStr(InsertData("EXEC InsertThanhVien ?,?,?,?,?",CommandType.Text,
                "@TenTV",obj.TenTV,OleDbType.VarWChar,
                "@NgaySinh",obj.NgaySinh,OleDbType.Date,
                "@DiaChi",obj.DiaChi,OleDbType.VarWChar,
                "@SDT",obj.SDT,OleDbType.VarChar,
                "@CV",obj.CV,OleDbType.Integer));
            if (obj.MaTV != null)
                return true;
            else
                return false;
        }
        public bool Update(ThanhVien obj)
        {
            return UpdateDeleteData("EXEC UpdateThanhVien ?,?,?,?,?,?", CommandType.Text,
                "@MaTV",obj.MaTV,OleDbType.VarChar,
                "@TenTV",obj.TenTV,OleDbType.VarWChar,
                "@NgaySinh", obj.NgaySinh, OleDbType.Date,
                "@DiaChi", obj.DiaChi, OleDbType.VarWChar,
                "@SDT",obj.SDT,OleDbType.VarChar,
                "@CV", obj.CV, OleDbType.Integer);
        }
        public bool Delete(ThanhVien obj)
        {
            return UpdateDeleteData("EXEC DeleteThanhVien ?",CommandType.Text,"@MaTV",obj.MaTV,OleDbType.VarChar);
        }
    }
}
